// mircoApp/customer_file/fille_in/fill_in.js
const skinBehavior = require("../../../utils/themeSkin/skinBehavior");
var app = getApp();
Page({

  behaviors: [skinBehavior],
  /**
   * 页面的初始数据
   */
  data: {
    teacher_name: "",
    name: "",
    price: "",
    remark: "",
    fileList: [],
    image_list: [],
    customer_archive_id: "",
    id: "",
    timerId: null

  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.data.customer_archive_id = options.customer_archive_id || '';
    this.data.id = options.id || '';
    if(options.id){
      wx.setNavigationBarTitle({
        title: '编辑项目',
      })
    }else{
      wx.setNavigationBarTitle({
        title: '添加项目',
      })
    }
    if (options.id) {
      this.getDetail()
    }
  },


  afterRead(event) {
    let {
      file
    } = event.detail;
    wx.showLoading({
      title: '上传中',
    })
    if (file && file.length) {
      for (let i = 0; i < file.length; i++) {
        this.onRead_1(file[i])
      }
    } else {
      this.onRead_1(file)
    }

  },
  onRead_1(file) {
    let urlStr = app.getNetAddresss('upload.uploadPic');
    wx.uploadFile({
      url: urlStr,
      filePath: file.url,
      name: 'file',
      formData: null,
      success: (res) => {
        wx.hideLoading();
        let _res = JSON.parse(res.data);
        this.data.image_list.push(_res.data.img_url);
        this.data.fileList.push({
          ...file,
          url: _res.data.img_url
        });
        this.setData({
          fileList: this.data.fileList
        });
      },
      fail: (err) => {
        wx.hideLoading()
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
 
  },
  getDetail() {
    app._postNetWork({
      url: app.getNetAddresss("plugin.customer-archive.frontend.customer-archive.get-project-info"),
      data: {
        project_id: this.data.id
      },
      success: resdata => {
        let {
          result,
          data,
          msg,
        } = resdata.data;
        if (result) {
          data.image_list && data.image_list.length !== 0 && data.image_list.forEach(item => {
            if(item){
              this.data.fileList.push({
                url: item
              })
              this.data.image_list.push(item);
            }
         
          })
          this.setData({
            fileList: this.data.fileList,
            name: data.name,
            remark: data.remark,
            price: data.price,
            teacher_name: data.teacher_name
          })
        } else {

          app.tips(msg);
        }
      }
    })
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},
  submit() {
    let json = {};
    let urlStr = '';
    if (this.data.id) {
      json = {
        form: {
          id: this.data.id,
          name: this.data.name,
          teacher_name:this.data.teacher_name,
          remark: this.data.remark,
          price: this.data.price,
          image_list: this.data.image_list
        },
      }
      urlStr = "plugin.customer-archive.frontend.customer-archive.save-project-info";
    } else {
      urlStr = 'plugin.customer-archive.frontend.customer-archive.add-project';
      json = {
        form: {
          customer_archive_id: this.data.customer_archive_id,
          name: this.data.name,
          remark: this.data.remark,
          teacher_name:this.data.teacher_name,
          price: this.data.price,
          image_list: this.data.image_list
        }
      }
    }
    app._postNetWork({
      url: app.getNetAddresss(urlStr),
      data: json,
      success: resdata => {
        let {
          result,
          msg,
        } = resdata.data;
        app.tips(msg);
        if (result) {
          this.data.timerId = setTimeout(() => {
            wx.navigateBack();
          }, 500);
        }
      }
    })

  },
  handle_delete(event) {
    let index = event.currentTarget.dataset.index;
    this.data.fileList.splice(index, 1);
    this.data.image_list.splice(index, 1);
    this.setData({
      fileList: this.data.fileList
    });
  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {
    this.data.timerId = null;
    clearTimeout(this.data.timerId);
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})